package com.lingxin.gps.wap.command;

import com.alibaba.fastjson.JSONObject;
import com.lingxin.gps.sdk.GpsDeviceApi;
import com.pisces.core.Assert;
import com.pisces.core.BaseCommand;

/**
 *
 */
public class CUserLogin extends BaseCommand {

	public static final int COOKIE_LIVE_IN_SECONDS = 7 * 24 * 3600;

	private String userName;
	private String password;

	@Override
	public void init() throws Exception {
		userName = getPara("userName");
		Assert.notBlank(userName, "用户名不能为空");

		password = getPara("password");
		Assert.notBlank(userName, "密码不能为空");
	}

	@Override
	public void doCommand() throws Exception {
		JSONObject resData = GpsDeviceApi.userLogin(userName, password);

		JSONObject currentUser = resData.getJSONObject("currentUser");
		context.setCookie("uid", currentUser.getString("id"), COOKIE_LIVE_IN_SECONDS);
		context.setCookie("token", currentUser.getString("token"), COOKIE_LIVE_IN_SECONDS);

		data.put("currentUser", currentUser);
	}

}
